package com.example.mobile1921605093zlj0604;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button StartSevice;
    private Button EndSevice;
    private Button btnStartSevice;
    private Button btnEndSevice;
    MusicSevice musicSevice;
    ServiceConnection serviceConnection;
    private Button btnPause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        final Intent intent = new Intent(MainActivity.this, MusicSevice.class);
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder iBinder) {
                musicSevice = ((MusicSevice.ServiceBinder) iBinder).getMusiceService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                musicSevice = null;
            }
        };
        btnStartSevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startService(intent);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });
        btnEndSevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicSevice.playMusic();
            }
        });
        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicSevice.pauseMusic();
            }
        });
    }
            private void initView() {
                textView = (TextView) findViewById(R.id.textView);
                StartSevice = (Button) findViewById(R.id.btnStartSevice);
                EndSevice = (Button) findViewById(R.id.btnEndSevice);
                btnStartSevice = (Button) findViewById(R.id.btnStartSevice);
                btnEndSevice = (Button) findViewById(R.id.btnEndSevice);
                btnPause = (Button) findViewById(R.id.btnPause);
            }
        }

